<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@ taglib prefix="t" tagdir="/WEB-INF/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
		java.util.List<com.group4.DAO.PostDAO> posts = com.group4.DB.Post.getPostList(Integer.parseInt(request.getParameter("id")));
		com.group4.DAO.ForumDAO forum = com.group4.DB.Forum.getForumWithID(Integer.parseInt(request.getParameter("id")));
		String topicName = forum.getForumHeadText();
		session.setAttribute("posts", posts);
		session.setAttribute("topicName", topicName);
		//String username = (String)session.getAttribute("username");
		//session.setAttribute("posts", posts);
%>
<script>
	function submitForm(thisObj, thisEvent) {
		var topicTitle = '<%= session.getAttribute("topicName") %>';
		var topicBody = $('#topicBody').val();
		var name = '<%= session.getAttribute("username") %>';
		var jsonDataObject = new Object();
		jsonDataObject.topicTitle = topicTitle;
		jsonDataObject.topicBody = topicBody;
		var jsonData = JSON.stringify(jsonDataObject);
		$.getJSON("/Signport/post/add", {topic:topicTitle, entry: topicBody, username: name}, function(data) {
			   if(data.error == undefined){
				   alert("Post successfully added");
			   }else{
				   alert(data.error);
			   }
				
			   $('#return_message').html(data.message);
			});
		return false;  // prevents the page from refreshing before JSON is read from server response
	}
</script>
<t:wrapper title="Forum Page">
	<div class="page-content">
		<div class="page-header">
			<h1>Topics</h1>
		</div>
		<div class="row">
			<div class="col-xs-6 widget-box transparent">
				<div class="widget-header widget-header-small">
					<h4>${topicName}</h4>	
				</div>
				<div class="widget-body">
					<div class="widget-main padding-8">
						<c:forEach items="${posts}" var="post" >
							<div class="profile-activity clearfix">
								<h6><a href="#">${forum.forumHeadText}</a></h6>
								<p>${post.postText}</p>
							</div>
						</c:forEach>

						<div class="form-group">
							<label for="topic_body">Post Body</label>
							<textarea name="topic_body" id="topicBody" class="form-control autosize-transition" placeholder="Content..." required></textarea>
						</div>
						
						<div class="form-actions">
							<input class="btn" type="submit" name="submit" value="Add Post" onclick="return submitForm(this, event);"> 
							
						</div>
					</div>		
				</div>
			</div>
		</div>
	</div>
</t:wrapper>